<!--template标签下一级只能有一个div-->
<template>
    <div>
        <div>
            <!--添加职位-->
            <el-input
                    class="addPositionManagerInput"
                    size="small"
                    placeholder="职位名称..."
                    prefix-icon="el-icon-plus"
                    v-model="position.name">
            </el-input>
            <el-button icon="el-icon-plus" size="small" type="primary" @click="addPosition">添加</el-button>
        </div>
        <div class="positionManagerListTab">
            <el-table
                    :data="positions"
                    border
                    stripe
                    @selection-change="handleSelectionChange"
                    size="small"
                    style="width: 65%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        width="150"
                        label="创建日期">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        width="150"
                        label="是否启用">
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" size="small" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <!--scope.$index 第几行-->
                        <!--scope.row 当前行的数据对象-->
                        <el-button
                                size="small"
                                @click="showEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="small"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-button size="small"  @click="deletePositionByIds" type="danger" class="mulDel" :disabled="this.multipleSelection.length==0">批量删除</el-button>
        </div>

        <el-dialog
                title="修改职位信息"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <el-tag size="small">职位名称</el-tag>
                <el-input size="small" v-model="editPosition.name" class="editPositionInput"></el-input>
            </div>
            <div>
                <el-tag size="small">是否启用</el-tag>
                <el-switch style="margin-left: 5px"
                        v-model="editPosition.enabled"
                        active-text="启用"
                        inactive-text="禁用">
                </el-switch>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
    <el-button size="small" type="primary" @click="doUpdate()">确 定</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>

    export default {
        name: "PositionManager",
        data(){
            return{
                position:{
                    name:'',
                },
                // 默认弹出框隐藏
                dialogVisible:false,
                editPosition:{
                    name:'',
                    enabled:null
                },
                positions: [],
                multipleSelection:[]
            }
        },
        mounted() {
            this.initPositions();
        },
        methods:{
            // 初始化职位信息,页面加载时需调用(是在mounted中调用)
            initPositions(){
                // 发送请求
                this.getRequest("/system/basic/position/").then(resp=>{
                    console.log(resp);
                    if(resp){
                        this.positions=resp.data;
                    }
                })
            },
            showEdit(index,data){
                this.dialogVisible=true;
                // 这样赋值在修改editPosition.name的时候也会修改data.name的值,所以换种方式
                // this.editPosition=data;
                Object.assign(this.editPosition,data);
            },
            doUpdate(){
              this.putRequest("/system/basic/position/",this.editPosition).then(resp=>{
                  if(resp){
                      this.dialogVisible=false;
                      this.initPositions();
                  }
              })
            },
            handleDelete(index,data){
                this.$confirm('此操作将永久删除'+data.name+'职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/position/"+data.id).then(resp=>{
                        if(resp){
                            this.initPositions();
                        }
                    });

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            deletePositionByIds(){
                let ids="?";
                this.multipleSelection.forEach(item=>{
                     ids+="ids="+item.id+"&";
                });
                this.deleteRequest("/system/basic/position/"+ids).then(resp=>{
                    if(resp){
                        this.initPositions();
                    }
                })
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            addPosition(){
                if(this.position.name){
                    //添加
                    this.postRequest("/system/basic/position/",this.position).then(resp=>{
                        if(resp){
                            this.initPositions();
                            this.position.name="";
                        }
                    })
                }else{
                    // 提示为空
                    this.$message.error("职位名称不能为空");
                }
            }
        }
    }
</script>

<style>
    .addPositionManagerInput{
        width: 260px;
        margin-right: 8px;
    }
    .positionManagerListTab{
        margin-top: 10px;
    }
    .editPositionInput{
        width: 200px;
        margin-left: 5px;
    }
    .mulDel{
        margin-top: 8px;
    }

</style>